#include "parameter_pll.h"

static pll_config_t const pll_info = {
	.magic = PARAMETER_PLL_MAGIC,
	.version = CONFIG_PLL_PARAMETER_VERSION,
	.size = CONFIG_PLL_PARAMETER_SIZE,
	.core_pll = CONFIG_CORE_FREQ,
	.lmu_pll = (CONFIG_DDR_FREQ / 4),
};

static void print_pll_pm(pll_config_t const * const pm)
{
	printf("=======================================\n");

	printf("PLL info : \n");

	printf(" magic = 0x%x\n", pm->magic);
	printf(" version = 0x%x\n", pm->version);

	printf(" pll_pll = %dMhz\n", pm->core_pll);
	printf(" lmu_pll  = %dMhz\n", pm->lmu_pll);
	printf(" ddr_freq  = %dMhz\n", CONFIG_DDR_FREQ);
	printf("=======================================\n");
}

static int pll_info_config(FILE *const fp, pm_info_t const *pm_d)
{
     if(NULL == fp){
        printf("fp is point to NULL\n");
		return -1;
    }

	fseek(fp, PM_PLL_OFFSET, 0);
	fwrite(&pll_info, sizeof(pll_info), 1, fp);
	return 0;
}

static int check_pll(FILE * const fp, uint64_t base)
{
	uint32_t magic;
	fseek(fp, base + PM_PLL_OFFSET, 0);
	fread(&magic, sizeof(magic), 1, fp);

	if(PARAMETER_PLL_MAGIC != magic)
		return -1;
	else
		return 0;
}

static int get_pll_info(FILE *const fp, parameter_base_t const *p_base)
{
	pll_config_t pll_parameter;
	pll_config_t const * const pm = &pll_parameter;

	if(NULL == fp){
		ERROR("fp is point NULL %s \n", __func__);
		return -1;
	}

	fseek(fp, p_base->addr + PM_PLL_OFFSET, 0);
	fread(&pll_parameter,sizeof(pll_config_t),1,fp);

	if(PARAMETER_PLL_MAGIC != pm->magic){
		ERROR("can not get right magic 0x%x, %s \n", pm->magic, __func__);
		return -1;
	}

	print_pll_pm(pm);
	return 0;
}

pm_list_fun_t pll_fun = {
	.cfg_fnc = pll_info_config,
	.ck_fnc = check_pll,
	.prf_fnc = get_pll_info,
};